import { _decorator, assetManager, director, easing, Layout, Node, tween, v3, Vec3 } from 'cc';
import { UIManager } from '../../../framework/ui/uimanager/UIManager';
import { VibrateManager } from '../../../common/src/manager/VibrateManager';
import { AudioManager } from '../../../common/src/manager/AudioManager';
import { MonoUIPanel } from '../../../../project-base/src/scripts/MonoUIPanel';
import { Sprite } from 'cc';
import { SpriteFrame } from 'cc';
import { GGAudio } from '../../../framework/core/base/GGAudio';
import DataMgr from '../../../utils/DataMgr';
import { GGStore } from '../../../framework/core/base/GGStore';
const { ccclass, property } = _decorator;

@ccclass('SettingPanel')
export class SettingPanel extends MonoUIPanel {
    @property({ type: Layout, tooltip:"内容布局."})
    content: Layout = null;   

    @property({ type: Node, tooltip:"节点-声音."})
    soundNode: Node = null;

    @property({ type: Node, tooltip:"节点-震动."})
    vibrateNode: Node = null;

    @property({ type: Node, tooltip: "节点-音乐." })
    musicNode: Node = null;

    @property({ type: Node, tooltip: "按钮-首页." })
    btnHome: Node = null;

    @property({ type: Node, tooltip: "按钮-重开." })
    btnRestart: Node = null;
    
    
    @property({ type: Node, tooltip: "游戏时" })
    gameType: Node = null;
 
    @property({ type: SpriteFrame, tooltip: "打开条" })
    onSprite: SpriteFrame = null;
    
    @property({ type: SpriteFrame, tooltip: "关闭条" })
    offSprite: SpriteFrame = null;

    start() {
        this.initUI()
    }
    // =======================================
    // 外部/内部属性定义(以public/private修饰)
    // =======================================
    // 数据对象缓存
    private data = null;

    private configs = {
        0: {
            content: { height: 323, paddingTop: 38, spacingY: 41 },
            bottomBtn: { active: true }
        },
        1: {
            content: { height: 402, paddingTop: 71, spacingY: 58 },
            bottomBtn: { active: false }
        },
    };

    // 开关位置
    private tempOnPos = v3(-42, 0, 0)
    private tempOffPos = v3(42, 0, 0)

    private info: {
        // type: SETTING_TYPE
    }

    private tempScale = new Vec3();
    bundleVer: string;
    // =======================================
    // 生命周期(模板方法，以on开头)
    // =======================================

    // =======================================
    // 自定义事件回调(以on开头)
    // =======================================
 
    
    // =======================================
    // 游戏逻辑方法(内部调用的用private修饰，外部调用和编辑器绑定的public修饰，废弃的方法不加修饰符方便后期移除)
    // =======================================

    public async setData(data) {
        this.info = data
        this.initUI()
    }

    /** 关闭面板. */
    public async onClosePanel() {
        AudioManager.playButton()
        await this.onExitAnimation();
        // gg.systemEvent.emit(EVENT_NAMES.ON_GAME_PAUSE, false);
        UIManager.close("setting:res/prefab/SettingPanel");
    }
    
    //界面打开的动画
    protected onEnterAnimation() {
        this.tempScale.set(0.01, 0.01, 1);
        this.dialog.scale = this.tempScale;
        tween(this.dialog)
        .call(() => { this.tempScale.set(1.08, 1.08, 1) })
        .to(0.2, { scale: this.tempScale }, { easing: easing.sineOut })
        .call(() => { this.tempScale.set(1, 1, 1) })
        .to(0.1, { scale: this.tempScale }, { easing: easing.sineIn })
        .start();
    }
    
    // 界面关闭的动画
    protected onExitAnimation(callback?) {
        return new Promise<void>((resolve) => {
            tween(this.dialog)
                .call(() => { this.tempScale.set(1.08, 1.08, 1) })
                .to(0.1, { scale: this.tempScale }, { easing: easing.quadOut })
                .call(() => { this.tempScale.set(0.01, 0.01, 1) })
                .to(0.2, { scale: this.tempScale }, { easing: easing.quadIn })
                .call(() => {
                    callback && callback();
                    resolve();
                })
                .start();
        });
    }

    /** 更新震动状态. */
    private updateVibrateState(isAnimation = false) {
        let onNode = this.vibrateNode.children[0]

        if (isAnimation) {
            if (VibrateManager.enable) {
                tween(onNode).to(0.2, { position: this.tempOnPos }).start()
                this.vibrateNode.getComponent(Sprite).spriteFrame = this.onSprite;
            }
            else {
                tween(onNode).to(0.2, { position: this.tempOffPos }).start()
                this.vibrateNode.getComponent(Sprite).spriteFrame = this.offSprite;
            }
        } else {
            if (VibrateManager.enable) {
                onNode.position = this.tempOnPos
                this.vibrateNode.getComponent(Sprite).spriteFrame = this.onSprite
            } else {
                onNode.position = this.tempOffPos
                this.vibrateNode.getComponent(Sprite).spriteFrame = this.offSprite
            }
        }
    }

    /** 更新声音状态. */
    private updateSoundState(isAnimation = false) {
        let onNode = this.soundNode.children[0]

        if (isAnimation) {
            if (AudioManager.enable) {
                tween(onNode).to(0.2, { position: this.tempOnPos }).start()
                this.soundNode.getComponent(Sprite).spriteFrame = this.onSprite;
            }
            else {
                tween(onNode).to(0.2, { position: this.tempOffPos }).start()
                this.soundNode.getComponent(Sprite).spriteFrame = this.offSprite;
            }
        } else {
            if (AudioManager.enable) {
                onNode.position = this.tempOnPos
                this.soundNode.getComponent(Sprite).spriteFrame = this.onSprite
            } else {
                onNode.position = this.tempOffPos
                this.soundNode.getComponent(Sprite).spriteFrame = this.offSprite
            }
        }
    }

    /** 更新音乐状态. */
    private updateMusicState(isAnimation = false) {
        let onNode = this.musicNode.children[0]

        if (isAnimation) {
            if (AudioManager.musicEnable) {
                tween(onNode).to(0.2, { position: this.tempOnPos }).start()
                this.musicNode.getComponent(Sprite).spriteFrame = this.onSprite;
            }
            else {
                tween(onNode).to(0.2, { position: this.tempOffPos }).start()
                this.musicNode.getComponent(Sprite).spriteFrame = this.offSprite;
            }
        } else {
            if (AudioManager.musicEnable) {
                onNode.position = this.tempOnPos
                this.musicNode.getComponent(Sprite).spriteFrame = this.onSprite
            } else {
                onNode.position = this.tempOffPos
                this.musicNode.getComponent(Sprite).spriteFrame = this.offSprite
            }
        }
    }

     /** 初始化UI. */
    private initUI() {
        // if(this.info){
        //     if (this.info.type == SETTING_TYPE.START) {
        //         this.gameType.active = false
        //     } else if (this.info.type == SETTING_TYPE.MAIN) {
        //         this.gameType.active = true
        //         gg.systemEvent.emit(EVENT_NAMES.ON_GAME_PAUSE, true);
        //     }
        // }
        // Timer.closeAllPropTimer();

        this.updateSoundState();
        this.updateVibrateState();
        this.updateMusicState();
    }

    // ---------------------------- 事件 ---------------------------- //

     /**点击音效 */
     onClickSound() {
        AudioManager.playButton();
        AudioManager.enable = !AudioManager.enable;
        this.updateSoundState(true);
    }

    /**点击音乐 */
    onClickMusic() {
        AudioManager.playButton();
        AudioManager.musicEnable = !AudioManager.musicEnable;

        this.updateMusicState(true);
    }

    /**点击震动 */
    onClickShake() {
        AudioManager.playButton();
        VibrateManager.enable = !VibrateManager.enable;
        VibrateManager.vibrateShort();
        this.updateVibrateState(true);
    }

    /**回到主页 */
    onClickHome() {
        AudioManager.playButton();
        this.onClosePanel()
    
        director.loadScene("start")
    }

    /**重新游戏 */
    onClickRestartGame() {
        AudioManager.playButton();

        // gg.systemEvent.emit(EVENT_NAMES.GAME_RESTART)
        this.onClosePanel()
    }

    /**关闭 */
    public onClickClose() {
        AudioManager.playButton();
        this.onClosePanel();
    }

    //退出游戏
    public onClickExitButton() {
        AudioManager.playButton();
        UIManager.open("exit:res/prefab/ExitPanel");
        this.onClosePanel();
    }

    //重新开始
    public onClickRestartButton() {
        AudioManager.playButton();
        // if(DataMgr.currentStamina > 0){
        // gg.systemEvent.emit(EVENT_NAMES.ON_GAME_START);
        // DataMgr.currentStamina--;
        // GGStore.set(GAME_DATA_ENUMS.SAVE_STAMINA, DataMgr.currentStamina);
        // this.onClosePanel();
        // }
        // else{
        //     UIManager.open("power:res/prefab/PowerPanel");
        // }
    }
}


